<?php
/**
 * 用户管理控制器
 * @authors 大个子 (354449881@qq.com)
 * @date    2018-01-12 09:09:09
 * @version 1.0
 */
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController {
	private $db;

	public function _initialize(){
		parent::_initialize();
		$this->db = D('User');
	}
	public function index() {
		// parent::userauth2(2);
		$where=array();
		if (I('get.keyword','','htmlspecialchars')) {
			$where['username'] = I('get.keyword','','htmlspecialchars');
		}
		$count = $this->db->where($where)->count();			//总记录数
		$url='/statics/image/Home/';
		$Page = new \Think\Page($count,15);				//实例化分页类 传入总记录数和每页显示的记录数
		$Page->setConfig('header','共<b>%TOTAL_ROW%</b>条记录&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页');
		$Page->setConfig('prev','<img src="'.$url.'prev.gif" border="0" title="上一页" />');
		$Page->setConfig('next','<img src="'.$url.'next.gif" border="0" title="下一页" />');
		$Page->setConfig('first','<img src="'.$url.'first.gif" border="0" title="第一页" />');
		$Page->setConfig('last','<img src="'.$url.'last.gif" border="0" title="最后一页" />');
		$Page->setConfig('theme', '%FIRST%%UP_PAGE%%LINK_PAGE%%DOWN_PAGE%%END%%HEADER%');//显示的先后顺序
        $Page->lastSuffix = false;//最后一页不显示为总页数
		$show = $Page->show();							//分页显示输出
		
		$volist = $this->db->relation(true)->where($where)->order('dtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$this->assign('volist',$volist);
		$this->assign('page',$show);					//输出分页
		$this->assign('keyword',$where['username']);
		$this->co = $count;//原系统中不知道做什么用的
		$this->display();
	}
	public function useradd() {
		// parent::win_userauth(3);
		$role=M('role');
		$volist=$role->where('status=0')->getField('id,rolename,status');

		$this->assign('volist',$volist);
		$this->display('add');
	}
	//添加用户
	public function useradd_do() {
		//验证用户权限
		// parent::userauth(3);

		// ajax提交不需要令牌验证
		C('TOKEN_ON',false);
		$data=array();
		if (IS_AJAX) {
			$data['roleid'] = I('post.roleid','','htmlspecialchars');
			$data['username'] = I('post.username','','htmlspecialchars');
			$data['password'] = I('post.password','');
			$data['email'] = I('post.email','');
			$data['description'] = I('post.description','','htmlspecialchars');
			$data['status'] = I('post.status','');
			var_dump($data);
			exit;

			//自动完成验证与新增
			if ($this->db->create($data)) {
				$uid=$this->db->add();
				$role=M('Role');
				$r=$role->where('id='.$data['roleid'])->getField('competence');
				//给新用户添加默认权限
				$this->db->where("id=$uid")->setField(array('competence' => $r));
				parent::operating(__ACTION__,0,'新增用户：'.$data['username']);
				R('Public/errjson',array('ok'));
			}else {
				parent::operating(__ACTION__,1,'新增失败：'.$data['username']);
				R('Public/errjson',array($this->db->getError()));
			}
		}else {
			parent::operating(__ACTION__,1,'非法请求');
			R('Public/errjson',array('非法请求'));
		}
	}
	//修改界面
	public function useredit() {
		// parent::win_userauth(4);
		$id = I('get.id','');
		if ($id=='' || !is_numeric($id)) {
			parent::operating(__ACTION__,1,'参数错误');
			$this->content='参数ID类型错误，请关闭本窗口';
			exit($this->display('Public:err'));
		}
		$id=intval($id);
		$role=M('role');
		//获取分类信息
		$volist=$role->where('status=0')->order('dtime')->getField('id,rolename,status');
		$data=array('id' => $id);
		if ($result=$this->db->where($data)->find()) {
			$this->result=$result;//这种操作相当于assign
			$this->volist=$volist;
			//获取权限数据
			$co=M('competence');
			$aulist=$co->where('sid=0 AND status=0')->order('dtime asc')->getField('id,cname,status');
			$slist=$co->where('sid<>0 AND status=0')->order('dtime asc')->getField('id,sid,cname,status');
			$this->aulist=$aulist;
			$this->slist=$slist;
			$this->display('edit');
		}else {
			parent::operating(__ACTION__,1,'数据不存在');
			$this->content='没有找到相关数据，请关闭本窗口';
			$this->display('Public:err');
		}
	}
	//用户修改处理
	public function useredit_do() {
		//验证用户权限
		// parent::userauth(4);

		// ajax提交不需要令牌验证
		C('TOKEN_ON',false);

		$data=array();
		if (IS_AJAX) {
			$data['id'] = I('post.id','');
			$data['roleid'] = I('post.roleid','');
			$data['username'] = I('post.username','','htmlspecialchars');
			$data['password'] = I('post.password','');
			$data['email'] = I('post.email','');
			$data['description'] = I('post.description','','htmlspecialchars');
			$data['competence'] = I('post.comp','');
			$data['status'] = I('post.status');
			$user=D('User');
			if ($user->create($data)) {
				$user->save();
				parent::operating(__ACTION__,0,'更改用户资料：'.$data['username']);
				R('Public/errjson',array('ok'));
			}else {
				parent::operating(__ACTION__,1,'更新失败：'.$data['username']);
				R('Public/errjson',array($user->getError()));
			}
		}else {
			parent::operating(__ACTION__,1,'非法请求：');
			R('Public/errjson',array('非法请求'));
		}
	}
	//个别信息修改
	public function uedit() {
		parent::win_userauth(18);
		$id = $_SESSION['ThinkUser']['ID'];
		$user=M('user');
		$data=array('ID' => $id);
		if ($result=$user->where($data)->find()) {
			$this->result=$result;
			//获取权限数据
			$this->display('uedit');
		}else {
			parent::operating(__ACTION__,1,'没有找到相关数据：'.$id);
			$this->content='没有找到相关数据，请关闭本窗口';
			$this->display('Public:err');
		}
	}
	//个别信息修改处理
	public function uedit_do() {
		//验证用户权限
		parent::userauth(18);
		$data=array();
		if ($this->isAjax()) {
			$data['ID'] = $_SESSION['ThinkUser']['ID'];
			$password = I('post.password','');
			$data['Email'] = I('post.email','');
			if (strlen($password)<6 || strlen($password)>18) {
				R('Public/errjson',array('请输入6～18位数的安全密码'));
			}
			if (!preg_match('/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/i',$data['Email'])) {
				R('Public/errjson',array('请输入正确的邮箱地址'));
			}
			$data['Password']=R('Public/sha1pow',array($password));
			$user=M('user');
			$where=array('ID' => $data['ID']);
			if ($user->where($where)->setField($data)) {
				parent::operating(__ACTION__,0,'修改密码成功');
				R('Public/errjson',array('ok'));
			}else {
				parent::operating(__ACTION__,1,'更新失败：'.$user->getError());
				R('Public/errjson',array($user->getError()));
			}
		}else {
			parent::operating(__ACTION__,1,'非法请求');
			R('Public/errjson',array('非法请求'));
		}
	}
	//删除数据
	public function userdel() {
		//验证用户权限
		// parent::userauth(5);
		//判断是否是ajax请求
		if (IS_AJAX) {
			if (I('post.post','')=='ok') {
				$id=I('post.id','');
				if ($id=='' || !is_numeric($id)) {
					parent::operating(__ACTION__,1,'参数错误');
					R('Public/errjson',array('参数ID类型错误'));
				}else {
					$id=intval($id);
					if ($id==1) {
						parent::operating(__ACTION__,1,'不能删除系统默认用户');
						R('Public/errjson',array('不能删除系统默认用户'));
					}
					$user=M('user');
					$where=array('id'=>$id);
					if ($user->where($where)->getField('id')) {
						$user->where($where)->delete();
						parent::operating(__ACTION__,0,'删除用户ID为：'.$id.'的数据');
						R('Public/errjson',array('ok'));
					}else {
						parent::operating(__ACTION__,1,'删除用户失败：'.$user->getError());
						R('Public/errjson',array($user->getError()));
					}
				}
			}else {
				parent::operating(__ACTION__,1,'非法请求');
				R('Public/errjson',array('非法请求'));
			}
		}else {
			parent::operating(__ACTION__,1,'非法请求');
			R('Public/errjson',array('非法请求'));
		}
	}
	//批量删除
	public function in_user_del() {
		//验证用户权限
		// parent::userauth(5);
		if (IS_AJAX) {
			if (!$delid=explode(',',I('post.delid',''))) {
				R('Public/errjson',array('请选中后再删除'));
			}
			//将最后一个元素弹出栈
			array_pop($delid);
			if (in_array(1,$delid)) {
				R('Public/errjson',array('不能删除ID号为1的数据'));
			}
			$id=join(',',$delid);
			$user=M('user');
			if ($user->delete("$id")) {
				parent::operating(__ACTION__,0,'批量删除ID为：'.$id.'的数据');
				R('Public/errjson',array('ok'));
			}else {
				parent::operating(__ACTION__,1,'批量删除用户失败：'.$user->getError());
				R('Public/errjson',array($user->getError()));
			}
		}else {
			parent::operating(__ACTION__,1,'非法请求');
			R('Public/errjson',array('非法请求'));
		}
	}
}
?>